package com.team.web;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.team.dao.IdeaDao;
import com.team.dao.UserDao;
import com.team.entity.Idea;
import com.team.entity.IdeaComment;
import com.team.entity.Project;
import com.team.entity.User;
import com.team.util.Const;
import com.team.util.StringUtil;
import com.team.util.TUtil;

public class NewIdeaServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
     @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
	String method = request.getParameter("_m");
	if("save".equals(method)){
		saveNewIdea(request,response);
	}else if("select".equals(method)){
		selectAllIdeas(request,response);
	}else if("edit".equals(method)){
		editIdea(request,response);
		
	}else if("update".equals(method)){
		updateIdea(request,response);
	}else if("find".equals(method)){
		find(request,response);
	}else {
		response.sendError(404,"request a error path");
		return;
		
	}
	
}

	private void find(HttpServletRequest request, 
			HttpServletResponse response) throws IOException {
		String ideaId = request.getParameter("ideaId");
		int id=Integer.parseInt(ideaId);
		
		HttpSession session = request.getSession();
		//Project project = (Project) session.getAttribute(Const.SESSION_PROJ);
		
		IdeaDao  dao = new IdeaDao();
		Idea findIdea  = dao.selectByIdeaId(id);
	
		if(findIdea != null){
			session.setAttribute(Const.SESSION_FINDIDEA, findIdea);
			
			int userId = findIdea.getUserId();
			UserDao ud = new UserDao();
			User userIdea = ud.selectById(userId);
			if(userIdea != null){
				session.setAttribute(Const.SESSION_USERIDEA, userIdea);
				
				response.sendRedirect("ideaComment.jspx?_m=select&ideaId="+ideaId);
				return;
				
			}else{
				
				System.out.println("user is null");
			}
			
			
			
		}else{
			System.out.println("idea find error");
		}
	}

	private void updateIdea(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String ideaId = request.getParameter("id");
		int id = Integer.parseInt(ideaId);
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		
		if(StringUtil.isEmpty(title)||StringUtil.isEmpty(content)){
			response.sendRedirect("editidea.jsp?code=1001");
			return ;
		}
		IdeaDao idea = new IdeaDao();
		HttpSession session = request.getSession();
		Project project = (Project) session.getAttribute(Const.SESSION_PROJ);
		int num = idea.updateIdea(title,content,id);
		if(num > 0){
			System.out.println("update idea succ");
			response.sendRedirect("newIdea.jspx?_m=select&id="+project.getId());
		}else {
			System.out.println(" update idea fail");
		}
	}

	private void editIdea(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String ideaId = request.getParameter("id");
		int id = Integer.parseInt(ideaId);
		
		IdeaDao idao = new IdeaDao();
		Idea idea = idao.selectByIdeaId(id);
		if(idea != null){
			HttpSession session = request.getSession();
			session.setAttribute(Const.SESSION_IDEA, idea);
			response.sendRedirect("editidea.jsp");
			return ;
		}else {
			System.out.println("this idea id not exists");
		}
		
		
	}

	private void selectAllIdeas(HttpServletRequest request,
		HttpServletResponse response) throws IOException {
		
		String projId = request.getParameter("id");
		int id = Integer.parseInt(projId);
		
		IdeaDao dao = new IdeaDao();
		
		List<Idea> list = dao.selectAllByProjId(id);
		if(list== null){
		 System.out.println("list null");
		}else {
			Map<Idea,User> map = new HashMap<Idea,User>();
			for(Idea idea:list){
			 User temp = new User();
			 UserDao ud = new UserDao();
			 temp = ud.selectById(idea.getUserId());
			 if(temp != null){
				 map.put(idea, temp);
			 }else{
				 System.out.println("temp is null");
			 }
			} 
			
		HttpSession session = request.getSession();
		session.setAttribute(Const.SESSION_MAPIDEA, map);
		
		
		session.setAttribute(Const.SESSION_IDEAList, list);
		response.sendRedirect("idea.jsp");
		
		return ;
		}
   }
   private void saveNewIdea(HttpServletRequest request,
		HttpServletResponse response) throws IOException {
	   
	   String content = request.getParameter("content");
	   String title = request.getParameter("title");
	   
	   if(StringUtil.isEmpty(content)||StringUtil.isEmpty(title)){
		   response.sendRedirect("idea.jsp?code=1001");
		   return ;
		}else{
			
			HttpSession session = request.getSession();
			Project project = (Project) session.getAttribute(Const.SESSION_PROJ);
			int projId = project.getId();
			User user = (User) session.getAttribute(Const.SESSION_USER);
			
			Idea de = new Idea();
			de.setContent(content);
			de.setTitle(title);
			de.setCreateTime(TUtil.getNow());
			de.setProjId(projId);
			de.setUserId(user.getId());
			
			IdeaDao dao = new IdeaDao();
			int num = dao.save(de);
			if(num > 0){
				System.out.println("idea save succ");
				response.sendRedirect("newIdea.jspx?_m=select&id="+projId);
				 return ;
			}else{
				System.out.println("idea save error");
				response.sendRedirect("idea.jsp");
			}
			
		}
		
	}
}
